package com.meantodo.demo.controller;

import com.meantodo.demo.domain.Goal;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author: tongqianwen
 * @Date: 2025/5/27
 */
@Slf4j
@Controller
@RequestMapping("/goal")
public class SetGoalController {

    @GetMapping
    public String showSetGoalForm(Model model){
        model.addAttribute("goal", new Goal());
        return "design";
    }

    @PostMapping
    public String processGoal(@Valid @ModelAttribute("goal") Goal goal, Errors errors){
        if(errors.hasErrors()){
            System.out.println("error occored "+ errors);
            return "design";
        }
        // todo save goals
        System.out.println("success submit "+goal);
        return "redirect:/goal/current";
    }
    @GetMapping("/current")
    public String submitGoal(@ModelAttribute("goal") Goal goal){
        System.out.println("success submit get "+goal);
        return "home";
    }
}
